package com.bingxue.edu.management.shop.service;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.management.shop.model.query.PayRecordQuery;
import com.bingxue.edu.management.shop.model.query.RefundRecordQuery;
import com.bingxue.edu.management.shop.model.resp.PayRecordListResp;
import com.bingxue.edu.management.shop.model.resp.PayRecordSummaryResp;
import com.bingxue.edu.management.shop.model.resp.RefundRecordListResp;
import com.bingxue.edu.management.shop.model.resp.RefundRecordSummaryResp;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.shop.model.entity.table.OrderTableDef.ORDER;
import static com.bingxue.edu.management.shop.model.entity.table.RefundTableDef.REFUND;
import static com.mybatisflex.core.query.QueryMethods.count;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 * @author panbo
 * @since 2024/11/28
 */
@Service
@RequiredArgsConstructor
public class PayRecordService {

    private final OrderService orderService;
    private final RefundService refundService;

    public Page<PayRecordListResp> getPayRecordPage(PayRecordQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery)
                .and(ORDER.PAY_OFF.eq(true))
                .orderBy(ORDER.ID.desc());
        return orderService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, PayRecordListResp.class);
    }

    public List<PayRecordListResp> getPayRecordList(PayRecordQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query)
                .and(ORDER.PAY_OFF.eq(true))
                .orderBy(ORDER.ID.desc());
        return orderService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, PayRecordListResp.class);
    }

    public PayRecordSummaryResp getPayRecordSummary(PayRecordQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query)
                .select(
                        count().as(PayRecordSummaryResp::getTotalCount),
                        sum(ORDER.ACTUAL_TOTAL_PRICE).as(PayRecordSummaryResp::getActualTotalPrice)
                )
                .and(ORDER.PAY_OFF.eq(true));
        return orderService.getMapper().selectOneByQueryAs(queryWrapper, PayRecordSummaryResp.class);
    }

    public Page<RefundRecordListResp> getRefundRecordPage(RefundRecordQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery)
                .and(REFUND.REFUND_OFF.eq(true))
                .orderBy(REFUND.ID.desc());
        return refundService.getMapper().paginateWithRelationsAs(pageQuery.toPage(), queryWrapper, RefundRecordListResp.class);
    }

    public List<RefundRecordListResp> getRefundRecordList(RefundRecordQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query)
                .and(REFUND.REFUND_OFF.eq(true))
                .orderBy(REFUND.ID.desc());
        return refundService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, RefundRecordListResp.class);
    }

    public RefundRecordSummaryResp getRefundRecordSummary(RefundRecordQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query)
                .select(
                        count().as(RefundRecordSummaryResp::getTotalCount),
                        sum(REFUND.REFUND_PRICE).as(RefundRecordSummaryResp::getTotalRefundPrice)
                )
                .and(REFUND.REFUND_OFF.eq(true));
        return refundService.getMapper().selectOneByQueryAs(queryWrapper, RefundRecordSummaryResp.class);
    }
}
